<?php namespace Common\Model; 

use \Hdphp\Model\Model;

class Goodslist extends Model{
	
	protected $table = "goods_list";
	
	
	public function addData(){
		//获得要添加的货品属于哪个商品 取得商品ID
		$gid = Q('post.goods_gid');
		//组合数据数组
		$data = array(
			'combine'=>implode(',', Q('post.spec')),
			'inventory'=>Q('post.inventory'),
			'number'=>Q('post.number'),
			'goods_gid'=>Q('post.goods_gid')
		);
		//把data数组添加进货品列表
		$this->add($data);
		//更新商品总库存
		$this->sumTotalSum($gid);
		return true;
	}
	
	public function editData(){
		//获得要编辑的货品的glid
		$glid = Q('post.glid');
		//组合数组
		$data = array(
			'combine'=>implode(',', Q('post.spec')),
			'inventory'=>Q('post.inventory'),
			'number'=>Q('post.number'),
			'goods_gid'=>Q('post.goods_gid')
		);
		//更改货品列表数据
		$this->where("glid={$glid}")->save($data);
		return true;
	}
	
	public function sumTotalSum($gid){
		//统计货品库存
		$totalnum = Db::table('goods_list')->where("goods_gid={$gid}")->sum('inventory');
		//实例化商品模型
		$goodsModel = new \Common\Model\Goods;
		//更新商品总库存
		$goodsModel->where("gid={$gid}")->update(array('totalnum'=>$totalnum));
	}
	
}


 ?>